<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-09-15
 * Time: 17:18
 */

namespace App\Http\Controllers\Api\V1\Front\Mall;


use App\Http\Controllers\Controller;
use App\Models\Cart;
use App\Models\Commodity;
use App\Models\CommoditySku;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class CartController extends Controller
{
    public function index(Request $request)
    {
        $abc =[];
        $data['cart'] = Cart::select('t1.cartId','t1.amounts','t1.createTime','t1.commodityId','t1.skuId','t2.commodityName',
            't2.commodityImage','t3.attrDesc','t3.commodityPrice','t3.discount','t2.saleStatus','t3.stock','t2.categoryId')
            ->from((new Cart())->getTable().' as t1')
            ->join((new Commodity())->getTable().' as t2','t1.commodityId','=','t2.commodityId')
            ->join((new CommoditySku())->getTable().' as t3','t1.skuId','=','t3.skuId')
            ->where(['userId'=>$request->userId])->get();
        if(count($data['cart'])){
            foreach ($data['cart'] as $k=>$v){
                $data['cart'][$k]['attrDesc'] = unserialize($v['attrDesc']);
                $abc[$k] = $v['categoryId'];
            }

            $commodity = Commodity::whereIn('categoryId',$abc)->limit(10)->get();
        }else{
            $commodity = Commodity::select()->orderBy(DB::raw('RAND()')) ->limit(10)->get();
        }

        $data['DoYouLike'] = $commodity;
        return $this->apiResponse($data);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make($data,[
            'cartId'        =>'integer',
            'commodityId'   =>'integer|required',
            'skuId'         =>'integer|required',
            'amounts'       =>'integer|required'
        ]);
        if($validator->fails()){
            return $this->apiResponse('', config('errorCode.INVALID_REQ_PARAMS'));
        }
        $sku = CommoditySku::find($data['skuId']);
        $userId = $request->userId;
        $data['skuDesc'] = $sku['attrDesc'];
        $data['price'] = $sku['discount'];
        $cartId = $data['cartId'];
        unset($data['cartId']);
        if($cartId){
            $status = Cart::where(['cartId'=>$cartId])->update($data);
        }else{
            $status = Cart::where(['commodityId'=>$data['commodityId'],'userId'=>$userId,'skuId'=>$data['skuId']])->first();
            if($status){
                Cart::where(['commodityId'=>$data['commodityId'],'userId'=>$userId,'skuId'=>$data['skuId']])->increment('amounts',$data['amounts']);
                return $this->apiResponse(true);
            }
            $data['userId'] = $userId;
            $status = Cart::create($data);
        }
        if(!$status){
            return $this->apiResponse('', config('errorCode.CATE_ADD_FAILED'));
        }
        return $this->apiResponse(true);
    }

    public function destroy($cartId)
    {
        $status = Cart::destroy($cartId);
        if(!$status){
            return $this->apiResponse('', config('errorCode.CATE_DELETE_FAILED'));
        }
        return $this->apiResponse(true);
    }

}